/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import data.ManagerDatabase;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author GREENWIND
 */
public class MyRenderer extends DefaultTreeCellRenderer {

        Icon tutorialIcon;
        Icon iconTable;
        Icon iconOpen;
        Icon iconClose;
        Icon iconKey;
        ArrayList<ManagerDatabase> list;

        public MyRenderer(Icon iconleaf,Icon icontable,Icon iconopen,Icon iconclose, 
                            Icon iconkey,ArrayList<ManagerDatabase> listDat) {
            tutorialIcon = iconleaf;
            iconTable = icontable;
            iconOpen = iconopen;
            iconClose = iconclose;
            iconKey = iconkey;
            list = listDat;
        }

        @Override
        public Component getTreeCellRendererComponent(
                JTree tree,
                Object value,
                boolean sel,
                boolean expanded,
                boolean leaf,
                int row,
                boolean hasFocus) {

            super.getTreeCellRendererComponent(
                    tree, value, sel,
                    expanded, leaf, row,
                    hasFocus);

            if (isDatabase(value)) {
                setIcon(tutorialIcon);

            } else if (isTable(value)) {
                setIcon(iconTable);
            } else if (isKey(value)) {
                setIcon(iconKey);
            } else {
                setLeafIcon(tutorialIcon);
                setOpenIcon(iconOpen);
                setClosedIcon(iconClose);
            }
            return this;
        }

        protected boolean isDatabase(Object value) {
            Object o = ((DefaultMutableTreeNode) value).getUserObject();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).GetName().equals(o.toString())) {
                    return true;
                }
            }
            return false;
        }

        protected boolean isTable(Object value) {
            Object o = ((DefaultMutableTreeNode) value).getUserObject();
            if (o.toString().equals("Table")) {
                return true;
            }
            return false;
        }

        protected boolean isKey(Object value) {
            Object o = ((DefaultMutableTreeNode) value).getUserObject();
            if (o.toString().equals("Primary Key")) {
                return true;
            }
            return false;
        }
    }